package com.ideal.manage.esms.controller.monitor;

import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.project.AccountProperty;
import com.ideal.manage.esms.bean.project.EquipmentManage;
import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.service.diagnostic.DiagnoseResultInfoService;
import com.ideal.manage.esms.service.diagnostic.DiagnoseService;
import com.ideal.manage.esms.service.monitor.*;
import com.ideal.manage.esms.service.project.EquipmentManageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Created By Dai On 2018/5/15
 * Remark: 电池包级监控 - 正式版（不是假数据啦~~~）
 */
@Controller
@RequestMapping("monitor")
public class MonitorForPackageController {
    private Logger logger = LoggerFactory.getLogger(MonitorController.class);

    @Resource
    private MonitorService monitorService;
    @Resource
    private EquipmentManageService equipmentManageService;
    @Resource
    private DiagnoseResultInfoService diagnoseResultInfoService;
    @Autowired
    private DiagnoseService diagnoseService;
    @Autowired
    private MonitorBlockHistoryService monitorBlockHistoryService;
    @Autowired
    private MonitorPackageHistoryService monitorPackageHistoryService;
    @Autowired
    private MonitorBlockChartService monitorBlockChartService;
    @Autowired
    private MonitorPackageService monitorPackageService;

    /**
     * 系统监控内嵌页面 - 电池包级
     *
     * @param model
     * @param id
     */
    @RequestMapping("monitor_web_for_package")
    public void monitorWebForPackage(Model model, String id) {
        // 1. 实体类
        EquipmentManage equipmentManage = equipmentManageService.findOne(id);
        model.addAttribute("equipmentManage", equipmentManage);
        if (StringUtils.isBlank(equipmentManage.getCode())) {
            model.addAttribute("message", "请在工程配置->对象管理->设备管理页面设置设备唯一标识，否则无法显示");
            return;
        }
        // 2. 电池包监控
        List<ParameterInfo> parameterInfoList = monitorPackageService.getRealTimeInfo(equipmentManage.getCode());
        model.addAttribute("parameterInfoList", parameterInfoList);
        // 3. 模组监控
        List<JSONObject> equipmentManageList = monitorPackageService.monitorGroupList(equipmentManage);
        model.addAttribute("equipmentManageList", equipmentManageList);
        // 4. 台账信息
        List<AccountProperty> accountPropertyListForPackage = monitorService.accountPropertyList(equipmentManage, 2, null);  // 电池包信息
        model.addAttribute("accountPropertyListForPackage", accountPropertyListForPackage);
        List<AccountProperty> accountPropertyListForBMS = monitorService.accountPropertyList(equipmentManage, 5, null);      // BMS信息
        model.addAttribute("accountPropertyListForBMS", accountPropertyListForBMS);
        // 5. 诊断分析
        JSONObject zdl = diagnoseService.getDiagnoseZdl(id); //总电量
        model.addAttribute("zdl", zdl);
        JSONObject zrl = diagnoseService.getDiagnoseDcbrl(id);//总容量
        model.addAttribute("zrl", zrl);
        JSONObject xhcx = diagnoseService.getDiagnoseXhcs(id);//循环次数
        model.addAttribute("xhcx", xhcx);
        JSONObject pc = diagnoseService.getDiagnosePianCha(id);
        model.addAttribute("pc", pc);
        JSONObject jz = diagnoseService.getDiagnoseJiaoZheng(id);
        model.addAttribute("jz", jz);

        JSONObject jsonObject = diagnoseService.getZDLInfo(id);
        model.addAttribute("jsonObject", jsonObject);
        // 6. 诊断分析 - 电池包一致性
        JSONObject batteryInfo = diagnoseService.getBatteryInfo(id);
        model.addAttribute("batteryInfo", batteryInfo);
    }

    /**
     * 实时监控 - 电压最高与最低 列表
     *
     * @return
     */
    @RequestMapping("get_voltage_data")
    @ResponseBody
    public List getVoltageData(String id) {
        return monitorService.getVoltageOrTemperatureData(id, "电压", 6, 1, 0);
    }

    /**
     * 实时监控 - 温度最高与最低 列表
     *
     * @return
     */
    @RequestMapping("get_temperature_data")
    @ResponseBody
    public List getTemperatureData(String id) {
        return monitorService.getVoltageOrTemperatureData(id, "温度", 6, 4, 0);
    }

    /**
     * 实时监控 - 单体电压/温度 排名 柱状图
     *
     * @param id
     * @return
     */
    @RequestMapping("get_top_and_bottom")
    @ResponseBody
    public JSONObject getTopAndBottom(String id) {
        return monitorBlockChartService.getTopAndBottom(id);
    }

    /**
     * 实时监控 - 电压/电流/SOC  今日曲线
     *
     * @param id
     * @return
     */
    @RequestMapping("get_today_line")
    @ResponseBody
    public JSONObject getTodayLine(String id) {
        return monitorBlockChartService.getPackageTodayLine(id);
    }

    /**
     * 实时监控 - 电压/温度 今日极值/极差曲线
     *
     * @param id
     * @return
     */
    @RequestMapping("get_today_bar")
    @ResponseBody
    public JSONObject getTodayBar(String id) {
        return monitorBlockChartService.getTodayBar(id);
    }

    /**
     * 历史数据
     *
     * @param id         设备id
     * @param objType    测点类别
     * @param name       测点名称
     * @param ranges     时间范围
     * @param freque     显示密度
     * @param show_type  显示类别
     * @param start_time 开始时间
     * @param end_time   结束时间
     * @return
     */
    @RequestMapping("history_data_equipment")
    @ResponseBody
    public Map historyDataEquipment(String id, int objType, String name, String ranges,
                                    String freque, String show_type, String start_time, String end_time,
                                    String type) {
        if (type.equals("block")) {
            return monitorBlockHistoryService.historyBlockDataEquipment(id, objType, name, ranges, freque,
                    show_type, start_time, end_time);
        } else {
            return monitorPackageHistoryService.historyPackageDataEquipment(id, objType, name, ranges, freque,
                    show_type, start_time, end_time);
        }
    }


    /**
     * 诊断分析 - 刷新数据（只有数据，没有图的那种）
     *
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("refresh_this_value")
    @ResponseBody
    public JSONObject refreshThisValue(String id, String name) {
        return diagnoseResultInfoService.refreshThisValue(id, name);
    }

    /**
     * 单体阻值频数/频率分布图
     *
     * @param id
     * @return
     */
    @RequestMapping("soc_correcting")
    @ResponseBody
    public JSONObject socCorrecting(String id) {
        return monitorPackageService.getSocFrequency(id);  // 单体SOC
    }

    /**
     * 单体阻值频数/频率分布图
     *
     * @param id
     * @return
     */
    @RequestMapping("dcir_correcting")
    @ResponseBody
    public JSONObject dcirCorrecting(String id) {
        return monitorPackageService.getCorrectingFrequency(id);  // 直流内阻DCIR
    }
}
